﻿
@{
    Layout = null;
}
<style>
    .ngdialog.ngdialog-theme-plain .ngdialog-content {
        width: 25% !important;
    }

    .table-adjust{
        border-collapse:collapse;
        width:800px;
    }

    .table-adjust td{
        border:1px solid silver;
        text-align:center;
    }
</style>

<div ng-controller="AdjustFlatController">
    <h3>公寓调整</h3>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <form name="formValidate" ng-submit="submitForm()" novalidate="" class="form-validate form-horizontal">
                    <div class="panel panel-default" ng-class="{ 'whirl standard': isLoading}">

                        <div class="panel-heading">
                            <div class="form-inline ng-pristine ng-valid">
                                <div class="form-group">
                                    <fieldset>

                                        <label class="col-sm-2 control-label">学号</label>
                                        <div class="col-sm-4">
                                            <input type="text" name="UserName" required="" id="UserName" />
                                        </div>
                                        <div class="col-sm-4">
                                            <h4 class="text-danger">*</h4>
                                        </div>
                                        <button type="button" class="btn btn-labeled btn-primary" ng-click="GetV_UserInfo()">
                                            <span class="btn-label">
                                                <em class="fa fa-search"></em>
                                            </span>搜索
                                        </button>
                                    </fieldset>

                                    <h5>原始公寓信息</h5>

                                    <div class="panel-body">
                                        <div class="table-responsive">
                                            <table class="table table-striped table-bproducted table-hover">
                                                <tr>
                                                    <td align="center">楼栋</td>
                                                    <td align="center">公寓</td>
                                                    <td align="center">系部</td>
                                                    <td align="center">教师</td>
                                                    <td align="center">班级</td>
                                                    <td align="center">床号</td>
                                                    <td align="center">柜号</td>
                                                </tr>
                                                <tr>
                                                    <td class="text-center">{{V_UserInfo.BuildingNO}}</td>
                                                    <td class="text-center">{{V_UserInfo.RoomNO}}</td>
                                                    <td class="text-center">{{V_UserInfo.DepartmentName}}</td>
                                                    <td class="text-center">{{V_UserInfo.RealName}}</td>
                                                    <td class="text-center">{{V_UserInfo.ClassName}}</td>
                                                    <td class="text-center">{{V_UserInfo.BedNO}}</td>
                                                    <td class="text-center">{{V_UserInfo.BedNO}}</td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <h5>新公寓信息</h5>
                                        <fieldset>
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">楼栋</label>
                                                <div class="col-sm-4">
                                                    <select ng-options="Building as Building.BuildingNO for Building in BuildingList track by Building.Id" ng-model="Building" class="form-control" name="BuildingNO" id="sel_Building" ng-change="BuildingChange()">
                                                        <option value="">请选择楼栋</option>
                                                    </select>
                                                    <span ng-show="validateInput('BuildingNO', 'required')" class="text-danger">请选择楼栋</span>
                                                </div>
                                                <div class="col-sm-4">
                                                    <h4 class="text-danger">*</h4>
                                                </div>
                                            </div>
                                        </fieldset>

                                        <fieldset>
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">公寓</label>
                                                <div class="col-sm-4">
                                                    <select ng-options="Room as Room.RoomNO for Room in RoomList track by Room.Id" ng-model="Room" class="form-control" name="RoomNO" id="sel_Room" ng-change="GetBedInfo(Room.Id)" ng-blur="RoomChange()">
                                                        <option value="">请选择公寓</option>
                                                    </select>
                                                    <span ng-show="validateInput('RoomNO', 'required')" class="text-danger">请选择公寓</span>
                                                </div>
                                                <div class="col-sm-4">
                                                    <h4 class="text-danger">*</h4>
                                                </div>
                                            </div>
                                        </fieldset>

                                        <fieldset>
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">系部</label>
                                                <div class="col-sm-4">
                                                    <select ng-options="Department as Department.DepartmentName for Department in DepartmentList track by Department.Id" ng-model="Department" class="form-control" name="DepartmentName" id="sel_Department" ng-change="DepartmentChange()">
                                                        <option value="">请选择系部</option>
                                                    </select>
                                                    <span ng-show="validateInput('DepartmentName', 'required')" class="text-danger">请选择系部</span>
                                                </div>
                                                <div class="col-sm-4">
                                                    <h4 class="text-danger">*</h4>
                                                </div>
                                            </div>
                                        </fieldset>
                                        @*<select ng-options="Teacher.Id as Teacher.RealName for Teacher in TeacherList" ng-model="V_UserInfo.TeacherID" class="form-control" name="TeacherName" id="sel_Teacher">
                                                <option value="">请选择教师</option>
                                            </select>*@

                                        <fieldset>
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">班级</label>
                                                <div class="col-sm-4">
                                                    <select ng-options="Class as Class.ClassName for Class in ClassList track by Class.Id" ng-model="Class" class="form-control" name="ClassName" required="" id="sel_Class">
                                                        <option value="">请选择班级</option>
                                                    </select>
                                                    <span ng-show="validateInput('ClassName', 'required')" class="text-danger">请选择班级</span>
                                                </div>
                                                <div class="col-sm-4">
                                                    <h4 class="text-danger">*</h4>
                                                </div>
                                            </div>
                                        </fieldset>

                                        <fieldset>
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">床号</label>
                                                <div class="col-sm-4">
                                                    <select ng-options="Bed as Bed.BedNO for Bed in BedList track by Bed.Id" ng-model="Bed" class="form-control" id="sel_Bed">
                                                        <option value="">选择床号</option>
                                                    </select>
                                                    <span ng-show="validateInput('BedNO', 'required')" class="text-danger">请选择床号</span>
                                                </div>
                                                <div class="col-sm-4">
                                                    <h4 class="text-danger">*</h4>
                                                </div>
                                            </div>
                                        </fieldset>

                                        <fieldset>
                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">柜号</label>
                                                <div class="col-sm-4">
                                                    <select ng-options="Bed as Bed.BedNO for Bed in BedList track by Bed.Id" ng-model="Bed" class="form-control">
                                                        <option value="">选择柜号</option>
                                                    </select>
                                                    <span ng-show="validateInput('BedNO', 'required')" class="text-danger">请选择柜号</span>
                                                </div>
                                                <div class="col-sm-4">
                                                    <h4 class="text-danger">*</h4>
                                                </div>
                                            </div>
                                        </fieldset>

                                        <button type="submit" class="btn btn-labeled btn-success" ng-click="AddFlatAdjustment()" ng-show="UserInfo.IsAdd || UserInfo.IsAdmin">
                                            <span class="btn-label">
                                                <em class="fa fa-plus"></em>
                                            </span>新增
                                        </button>

                                    </div>
                                </div>
                            </div>
                            <div class="panel-body">

                                <div class="table-responsive">
                                    <table ng-table="tableParams" class="table table-striped table-bproducted table-hover">
                                        <tbody>
                                            <tr ng-repeat="item in $data" ng-show="UserInfo.IsQuery || UserInfo.IsAdmin">

                                                <td data-title="'大楼号'" class="text-center">{{item.BuildingNO}}</td>
                                                <td data-title="'公寓号'" class="text-center">{{item.RoomNO}}</td>
                                                <td data-title="'学院'" class="text-center">{{item.DepartmentName}}</td>
                                                <td data-title="'班级'" class="text-center">{{item.ClassName}}</td>
                                                <td data-title="'班主任'" class="text-center">{{item.TeacherName}}</td>
                                                <td data-title="'学号'" class="text-center">{{item.StudentNO}}</td>
                                                <td data-title="'姓名'" class="text-center">{{item.StudentName}}</td>
                                                <td data-title="'床号'" class="text-center">{{item.BedNO}}</td>
                                                <td data-title="'柜号'" class="text-center">{{item.RoomAttrName}}</td>
                                                <td data-title="'操作'" nowrap="nowrap" class="text-center">
                                                    @*<button type="button" ng-click="BuildingLocation(item.BuildingNO)" class="btn btn-sm btn-info" title="位置">
                                                            <em class="fa fa-location-arrow"></em>
                                                        </button>*@
                                                    <button type="button" ng-click="RoomEdit(item.Id)" class="btn btn-sm btn-edit" title="编辑" ng-show="UserInfo.IsEdit ||UserInfo.IsAdmin">
                                                        <em class="fa fa-edit"></em>
                                                    </button>
                                                    <button type="button" ng-click="RoomDel($index, item.Id)" class="btn btn-sm btn-danger" title="删除" ng-show="UserInfo.IsDel || UserInfo.IsAdmin">
                                                        <em class="fa fa-trash"></em>
                                                    </button>

                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>

                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css" />
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.0&key=56aaa1c52d140b9b3276d2893997b0ba"></script>
<script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>

<script type="text/javascript">
    App.controller('AdjustFlatController', ["$scope", "$state", "$http", 'ngTableParams', 'ngTableDataService', "$stateParams", "Notify", "$filter",
    function ($scope, $state, $http, ngTableParams, ngTableDataService, $stateParams, Notify, $filter) {
            'use strict';

            $scope.IsRightBuildingNO = false;
            $scope.IsRightUnitNumber = false;
            $scope.IsRightFloorNumber = false;
            $scope.IsRightRoomNumber = false;
            $scope.IsCheckBuildingNO = false;
            $scope.Longitude = 0;
            $scope.Latitude = 0;

            $scope.uploading = false;
            $scope.fileUploadEndFlag = false;
            $scope.Keywords = {
                BulidingID: 0,
                RoomID: 0,
                DepartmentID: 0,
                TeacherID: 0,
                ClassID: 0,
                StudentNO: "",
                StudentName: "",
            };
        

            $scope.validateInput = function (name, type) {
                var input = $scope.formValidate[name];
                return (input.$dirty || $scope.submitted) && input.$error[type];
            };

            //数据查询
            $scope.tableParams = new ngTableParams({
                page: 1,
                count: 10,
                sorting: {
                    BuildingNO: 'asc',     // 初始化排序
                    RoomNO: 'asc'     //
                }
            }, {
                getData: function ($defer, params) {

                    var Building = document.getElementById("sel_Building");
                    var BuildingIndex = Building.selectedIndex;
                    $scope.Keywords.BulidingID = Building.options[BuildingIndex].value;

                    var Room = document.getElementById("sel_Room");
                    var RoomIndex = Room.selectedIndex;
                    $scope.Keywords.RoomID = Room.options[RoomIndex].value;

                    var Department = document.getElementById("sel_Department");
                    var DepartmentIndex = Department.selectedIndex;
                    $scope.Keywords.DepartmentID = Department.options[DepartmentIndex].value;

                    //var Teacher = document.getElementById("sel_Teacher");
                    //var TeacherIndex = Teacher.selectedIndex;
                    //$scope.Keywords.TeacherID = Teacher.options[TeacherIndex].value;

                    var Class = document.getElementById("sel_Class");
                    var ClassIndex = Class.selectedIndex;
                    $scope.Keywords.ClassID = Class.options[ClassIndex].value;

                    $scope.Keywords.StudentNO = $scope.StudentNO;
                    $scope.Keywords.StudentName = $scope.StudentName;

                    $scope.isLoading = true;
                    var requestParams = {
                        Skip: (params.page() - 1) * params.count(),
                        Take: params.count(),
                        Keywords: $scope.Keywords,
                        UnitLevel: $scope.UnitLevel
                    };

                    $http.post('/StudentBed/GetStudentBedList', ngTableDataService.getServerSideParams(params.orderBy().toString(), params, requestParams))
                        .success(function (data, status) {

                            params.total(data.Total);
                            $defer.resolve(data.Results);
                            $scope.UserInfo = data._UserInfo;
                            $scope.BuildingList = data.BuildingList;
                            $scope.RoomList = data.RoomList;
                            $scope.RoomAllList = data.RoomList;

                            $scope.DepartmentList = data.DepartmentList;
                            $scope.ClassList = data.ClassList;
                            $scope.ClassAllList = data.ClassList;
                            $scope.TeacherList = data.TeacherList;

                            $scope.TeacherAllList = data.TeacherList;
                            $scope.isLoading = false;
                        });
                }
            });

            $scope.GetV_UserInfo = function () {
                $http.post("/Building/GetV_UserInfo", { name: $("#UserName").val() }).then(function (response) {
                    $scope.V_UserInfo = angular.copy(response.data.data[0]);

                    $http.post("/StudentBed/GetStudentBedInitDetail", { id: $scope.V_UserInfo.BedID }).then(function (response) {
                        $scope.StudentBed = angular.copy(response.data.StudentBed);
                    });
                });
            }

            //通过改变楼宇信息，获取公寓信息
            $scope.BuildingChange = function () {
                $scope.RoomList = $scope.RoomAllList;
                var selIndex = document.getElementById("sel_Building").selectedIndex;
                if (selIndex == 0) {

                }
                else {
                    $scope.RoomList = $scope.RoomList.filter(function (x) {

                        return x.BuildingID === $scope.Building.Id;
                    });
                }
            };

            //通过改变学院信息，获取老师班级信息
            $scope.DepartmentChange = function () {
                if ($scope.V_UserInfo == null) {
                    Notify.alert("请输入学生学号并搜索原始住宿信息", { status: 'warning' });
                    return;
                }

                $scope.ClassList = $scope.ClassAllList;
                $scope.TeacherList = $scope.TeacherAllList;
                var selIndex = document.getElementById("sel_Department").selectedIndex;
                if (selIndex == 0) {

                }
                else {
                    $scope.ClassList = $scope.ClassList.filter(function (x) {
                        return x.DepartmentId === $scope.V_UserInfo.DepartmentID;
                    });
                    $scope.TeacherList = $scope.TeacherList.filter(function (x) {
                        return x.DepartmentId === $scope.V_UserInfo.DepartmentID;
                    });
                }
            };

            //通过改变公寓信息，获取床位信息
            $scope.RoomChange = function () {
                debugger
                $scope.BedList = $scope.BedAllList;
                var selIndex = document.getElementById("sel_Bed").selectedIndex;
                if (selIndex == -1) {

                }
                else {

                    $scope.BedList = $scope.BedList.filter(function (x) {
                        return x.RoomId === $scope.Room.Id;
                    });
                }
                //if ($scope.BedList.length == 0) {
                //    $scope.Bed.Id = 0;
                //}
            };

            //获取床位和柜号信息
            $scope.GetBedInfo = function (RoomId) {

                $http.post("/StudentBed/GetBedInfo", { RoomId: RoomId }).then(function (returnData) {
                    $scope.BedList = returnData.data.BedList;
                    $scope.BedAllList = returnData.data.BedList;
                    return true;
                });
            };

            $http.post("/DormAdjust/GetDormAdjust", { id: 0 }).then(function (response) {
                $scope.DormAdjust = angular.copy(response.data);

                console.log(JSON.stringify($scope.DormAdjust));

                if ($scope.DormAdjust == null) {
                    $scope.DormAdjust = {};
                }
            });

            $scope.AddFlatAdjustment = function () {
                if ($scope.V_UserInfo == null) {
                    Notify.alert("请输入学生学号并搜索原始住宿信息", { status: 'warning' });
                    return;
                }

                var adjust = {
                    StudentId: $scope.V_UserInfo.ID,
                    OldBuildingId: $scope.V_UserInfo.BuildingID,
                    NewBuildingId: $("#sel_Building").val(),
                    OldRoomId: $scope.V_UserInfo.RoomID,
                    NewRoomId: $("#sel_Room").val(),
                    OldBedId: $scope.V_UserInfo.BedID,
                    NowBedId: $("#sel_Bed").val(),
                    TeacherApproval: "",
                    TeacherId: $scope.V_UserInfo.TeacherID,
                    CollegeApproval: "",
                    CollegeId: 0,
                    SchoolWorkApproval: "",
                    SchoolWorkId: 0,
                    AddTime: new Date(),
                    EditTime: new Date(),
                    AddUserId: 0,
                    EditUserId: 0,
                    Type: 1,
                    ApprovalReason: "",
                    RejectReason: ""
                }

                debugger

                //添加公寓调整记录
                $http.post("/DormAdjust/AddDormAdjust", { model: adjust }).then(function (response) {

                })

                //$scope.StudentBed
                $http.post("/DormAdjust/UpdateStudentBedInfo", { model: $scope.StudentBed, bedId: $scope.V_UserInfo.BedID }).then(function (response) {

                })
            }

            

            //数据初始化
            $http.post("/Building/GetBuildingList", { Take: 100, OrderByFields: "Id" }).then(function (response) {

                $scope.BuildingList = angular.copy(response.data.Results);
                if ($scope.BuildingList == null) {
                    $scope.BuildingList = {};
                }

                console.log(JSON.stringify(response.data.Results));
                $scope.isLoading = false;
            });

            // 保存点击
            $scope.save = function () {
                $scope.submitted = true;
                //判断大楼号
                var testBuildingNO = /^[0-9]*$/;

                if (testBuildingNO.test($scope.Building.BuildingNO) === false) {
                    $scope.IsRightBuildingNO = true;
                    var txt = document.getElementById('BuildingNO')
                    txt.focus();
                    txt.select();
                    return false;
                } else {
                    $scope.IsRightBuildingNO = false;
                }

                //检查大楼是否存在
                $http.post("/Building/BuildingCheck", { BuildingNO: $scope.Building.BuildingNO }).then(function (returnData) {
                    if (returnData.data.isSuccess) {

                        $scope.IsCheckBuildingNO = false;

                        $scope.isLoading = true;
                        if ($scope.formValidate.$valid) {
                            $http.post("/Building/BuildingSave", $scope.Building).then(function (returnData) {
                                if (returnData.data.isSuccess) {
                                    Notify.alert('操作成功!', { status: 'success' });
                                    $state.go("app.buildinglist");
                                } else {
                                    Notify.alert(returnData.data.msg, { status: 'warning' });
                                    $scope.isLoading = false;
                                }
                            });

                        } else {
                            $scope.isLoading = false;
                        }
                    } else {
                        $scope.IsCheckBuildingNO = true;
                        var txt = document.getElementById('BuildingNO')
                        txt.focus();
                        txt.select();
                        return false;
                    }
                });


            };


            ////地址定位获取地址
            //$scope.getAddr = function () {
            //    var mapObj = new AMap.Map('container');
            //    mapObj.plugin('AMap.Geolocation', function () {
            //        var geolocation = new AMap.Geolocation({
            //            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            //            timeout: 10000,          //超过10秒后停止定位，默认：无穷大
            //            maximumAge: 0,           //定位结果缓存0毫秒，默认：0
            //            convert: true,           //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
            //            showButton: true,        //显示定位按钮，默认：true
            //            buttonPosition: 'LB',    //定位按钮停靠位置，默认：'LB'，左下角
            //            buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            //            showMarker: true,        //定位成功后在定位到的位置显示点标记，默认：true
            //            showCircle: true,        //定位成功后用圆圈表示定位精度范围，默认：true
            //            panToLocation: true,     //定位成功后将定位到的位置作为地图中心点，默认：true
            //            zoomToAccuracy: true      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            //        });
            //        mapObj.addControl(geolocation);
            //        geolocation.getCurrentPosition();
            //        AMap.event.addListener(geolocation, 'complete', function (data) {
            //            $scope.Longitude = data.position.getLng();
            //            $scope.Latitude = data.position.getLat();
            //            //获取经度纬度后改


            //        });//返回定位信息
            //        AMap.event.addListener(geolocation, 'error', function (data) {
            //            console.log('定位失败');
            //        });      //返回定位出错信息
            //    });

            //};

            //返回列表
            $scope.cancel = function () {
                $state.go("app.buildinglist");
            };

        }]);
</script>
